﻿namespace Z2.Editor
{
	using System;
	using System.Collections.Generic;
	using System.IO;
	using System.Text;
	using System.Xml;
	using System.Xml.Serialization;

	[Serializable]
	public class LastMap
	{
		private string m_fileName;

		public LastMap()
		{
		}

		[XmlAttribute]
		public string FileName
		{
			get
			{
				return this.m_fileName;
			}
			set
			{
				this.m_fileName = value;
			}
		}
		private static string GetFileName()
		{
			string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"ZelviciEditor\");
			if (!Directory.Exists(path))
			{
				Directory.CreateDirectory(path);
			}
			path = Path.Combine(path, "LastMaps.xml");
			return path;
		}

		public static void Save(List<LastMap> maps)
		{
			using (XmlTextWriter writer = new XmlTextWriter(LastMap.GetFileName(), Encoding.UTF8))
			{
				XmlSerializer ser = new XmlSerializer(typeof(List<LastMap>));
				ser.Serialize(writer, maps);
			}
		}

		public static List<LastMap> GetLastMaps()
		{
			List<LastMap> maps = null;
			if (!File.Exists(LastMap.GetFileName()))
			{
				return new List<LastMap>();
			}
			using (XmlTextReader reader = new XmlTextReader(LastMap.GetFileName()))
			{
				XmlSerializer ser = new XmlSerializer(typeof(List<LastMap>));
				maps = ser.Deserialize(reader) as List<LastMap>;
				maps.Reverse();
			}
			if (maps == null)
			{
				maps = new List<LastMap>();
			}

			return maps;
		}
	}
}
